package study.chapter03.stream;

import study.chapter03.stream.interfaces.DirMethods;

import java.io.File;

public class TestDir implements DirMethods {
    public static void main(String[] args) {
        TestDir test = new TestDir();
//        test.testMkdirs();
//        test.testReadDir();
        //test.testReadDir();
        File dirName = new File("F:/0temp/hello");
        test.testDeleteDir(dirName);
    }
    @Override
    public void testMkdir() {
        File file = new File("F:/0temp/hello");
        boolean b = file.mkdir();
        if (b){
            System.out.println("目录创建成功");
        }else {
            System.out.println("目录创建失败");
        }
    }

    @Override
    public void testMkdirs() {
        File file = new File("F:/0temp/hello/hello1/hello2");
        boolean b = file.mkdirs();
        if (b){
            System.out.println("目录创建成功");
        }else {
            System.out.println("目录创建失败");
        }
    }

    @Override
    public void testReadDir() {
        String dirName = "F:/0temp";
        File file = new File(dirName);
        String[] list = file.list();
        for (String s : list) {
            File f = new File(dirName + "/" + s);
            if (f.isDirectory()){
                System.out.println(s + "是一个目录");
            }else if (f.isFile()){
                System.out.println(s + "是一个文件");
            }else {
                System.out.println("未知类型" + s);
            }
        }
    }

    @Override
    public void testDeleteDir(File folder) {
        File[] files = folder.listFiles();
        Boolean b;
        if (files != null){
            for (File f : files){
                if (f.isDirectory()){
                    testDeleteDir(f);
                }else {
                    b = f.delete();
                    if (b){
                        System.out.println(f.getName() + "删除成功");
                    }else {
                        System.out.println(f.getName() + "删除失败");
                    }
                }
            }
        }
        b = folder.delete();
        if (b){
            System.out.println(folder.getName() + "删除成功");
        }else {
            System.out.println(folder.getName() + "删除失败");
        }
    }
}
